package com.jams.goodox.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.webkit.DownloadListener;

import com.github.lzyzsd.library.BridgeWebView;
import com.github.lzyzsd.library.DefaultHandler;
import com.jams.library.image.ImagePagerActivity;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Entities;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Cosmo on 2015/10/12.
 */
public class WebViewUtils {

	public static void setDataScriptListener(final BridgeWebView clause_webview, String title, String intime, String content) {
		final Context context = clause_webview.getContext();
		setScriptListener(clause_webview);
		// 加载HTML字符串进行显示
		InputStream input;
		try {
			input = clause_webview.getContext().getResources().getAssets().open("content_template.html");
			Document doc = Jsoup.parse(input, "UTF-8", "");

			Elements elementsContainingOwnText = doc.getElementsContainingOwnText("{{title}}");
			for (Element link : elementsContainingOwnText) {
				link.text(title);
			}
			Elements elements2 = doc.getElementsContainingOwnText("{{ptime}}");
			for (Element link : elements2) {
				link.text(intime);
			}
			Elements elements3 = doc.getElementsContainingOwnText("{{body}}");
			for (Element link : elements3) {
				link.text(content);
			}
			// 清除jsoup的转义功能
			Entities.EscapeMode.base.getMap().clear();
			String str = doc.html();
			// CustomToast.makeToast(mContext,str);
			clause_webview.loadData(str, "text/html;charset=UTF-8", null);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}

	public static void setWebOnlyBody(final BridgeWebView clause_webview, String content) {

		final Context context = clause_webview.getContext();
		setScriptListener(clause_webview);
		// 加载HTML字符串进行显示
		InputStream input;
		try {
			input = clause_webview.getContext().getResources().getAssets().open("body_template.html");
			Document doc = Jsoup.parse(input, "UTF-8", "");

			Elements elements3 = doc.getElementsContainingOwnText("{{body}}");
			for (Element link : elements3) {
				link.text(content);
			}

			// 清除jsoup的转义功能
			// CustomToast.makeToast(mContext,str);
			Entities.EscapeMode.base.getMap().clear();
			String str = doc.html();
			clause_webview.loadData(str, "text/html;charset=UTF-8", null);
			setScriptListener(clause_webview);
		} catch (IOException e1) {
			e1.printStackTrace();
		}


	}


	public static void setCustomDataScriptListener(final BridgeWebView clause_webview, String title, String time, String childTitle, String count, String content) {
		final Context context = clause_webview.getContext();
		setScriptListener(clause_webview);
		// 加载HTML字符串进行显示
		InputStream input;
		try {
			input = clause_webview.getContext().getResources().getAssets().open("new_template.html");
			Document doc = Jsoup.parse(input, "UTF-8", "");
			Elements elementsContainingOwnText = doc.getElementsContainingOwnText("{{title}}");
			for (Element link : elementsContainingOwnText) {
				link.text(title);
			}
			Elements elements2 = doc.getElementsContainingOwnText("{{amount}}");
			for (Element link : elements2) {
				link.text(count);
			}
			Elements elements3 = doc.getElementsContainingOwnText("{{body}}");
			for (Element link : elements3) {
				link.text(content);
			}

			Elements elements4 = doc.getElementsContainingOwnText("{{community}}");
			for (Element link : elements4) {
				link.text(childTitle);
			}

			Elements elements5 = doc.getElementsContainingOwnText("{{time}}");
			for (Element link : elements5) {
				link.text(time);
			}
			// 清除jsoup的转义功能
			Entities.EscapeMode.base.getMap().clear();
			String str = doc.html();
			// CustomToast.makeToast(mContext,str);
			clause_webview.loadData(str, "text/html;charset=UTF-8", null);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * 此用于包含阅读数的完整h5模板
	 * @param clause_webview 加载webview的控件
	 * @param title 标题
	 * @param time 时间
	 * @param count 计数
	 * @param content 内容
	 */
	public static void setReadDataScriptListener(final BridgeWebView clause_webview, String title, String time, String count, String content) {
		setScriptListener(clause_webview);
		// 加载HTML字符串进行显示
		InputStream input;
		try {
			input = clause_webview.getContext().getResources().getAssets().open("read_title_body_template.html");
			Document doc = Jsoup.parse(input, "UTF-8", "");
			Elements elementsContainingOwnText = doc.getElementsContainingOwnText("{{title}}");
			for (Element link : elementsContainingOwnText) {
				link.text(title);
			}
			Elements elements2 = doc.getElementsContainingOwnText("{{amount}}");
			for (Element link : elements2) {
				link.text(count);
			}
			Elements elements3 = doc.getElementsContainingOwnText("{{body}}");
			for (Element link : elements3) {
				link.text(content);
			}

			Elements elements5 = doc.getElementsContainingOwnText("{{time}}");
			for (Element link : elements5) {
				link.text(time);
			}
			// 清除jsoup的转义功能
			Entities.EscapeMode.base.getMap().clear();
			String str = doc.html();
			// CustomToast.makeToast(mContext,str);
			clause_webview.loadData(str, "text/html;charset=UTF-8", null);
		} catch (IOException e1) {
			e1.printStackTrace();
		}


	}

	public static void setDataScriptListener2(final BridgeWebView clause_webview, String title, String intime, String content) {
		final Context context = clause_webview.getContext();
		setScriptListener(clause_webview);
		// 加载HTML字符串进行显示
		InputStream input;
		try {
			input = clause_webview.getContext().getResources().getAssets().open("content_template2.html");
			Document doc = Jsoup.parse(input, "UTF-8", "");

			Elements elementsContainingOwnText = doc.getElementsContainingOwnText("{{title}}");
			for (Element link : elementsContainingOwnText) {
				link.text(title);
			}
			Elements elements2 = doc.getElementsContainingOwnText("{{ptime}}");
			for (Element link : elements2) {
				link.text(intime);
			}
			Elements elements3 = doc.getElementsContainingOwnText("{{body}}");
			for (Element link : elements3) {
				link.text(content);
			}
			// 清除jsoup的转义功能
			Entities.EscapeMode.base.getMap().clear();
			String str = doc.html();
			// CustomToast.makeToast(mContext,str);
			clause_webview.loadData(str, "text/html;charset=UTF-8", null);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}

	public static void setDataScriptListener(final BridgeWebView clause_webview, String title, String content) {
		final Context context = clause_webview.getContext();

		// 加载HTML字符串进行显示
		InputStream input;
		try {
			input = context.getResources().getAssets().open("public_content.html");
			Document doc = Jsoup.parse(input, "UTF-8", "");
			Elements elementsContainingOwnText = doc.getElementsContainingOwnText("{{title}}");
			for (Element link : elementsContainingOwnText) {
				link.text(title);
			}
			Elements elements2 = doc.getElementsContainingOwnText("{{body}}");
			for (Element link : elements2) {
				link.text(content);
			}
			// 清除jsoup的转义功能
			Entities.EscapeMode.base.getMap().clear();
			String str = doc.html();
			clause_webview.loadData(str, "text/html;charset=UTF-8", null);
			setScriptListener(clause_webview);
		} catch (Exception e2) {
			e2.printStackTrace();
		}

	}

	public static void setScriptListener(final BridgeWebView clause_webview) {
		final Context context = clause_webview.getContext();

		DefaultHandler defaultHandler = new DefaultHandler(clause_webview.getContext());
		defaultHandler.setOnScriptCallBack(new DefaultHandler.OnScriptCallBack() {
			@Override
			public void onScriptCallBack(String callback) {
				if (TextUtils.isEmpty(callback) || !callback.contains(",")) {
					return;
				}
				String[] source = callback.split(",");
				if (source == null || source.length == 0 || source.length == 1) {
					return;
				}
				int position = 0;

				List<String> urls = new ArrayList<String>();
				for (int i = 0; i < source.length; i++) {
					if (i == 0) {
						try {
							position = Integer.parseInt(source[0]);
						} catch (Exception e) {
							e.printStackTrace();
						}

					} else if (i > 0) {
						urls.add(source[i]);
					}
				}
				Intent intent = new Intent();
				intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, (Serializable) urls);
				intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
				intent.setClass(context, ImagePagerActivity.class);
				context.startActivity(intent);
			}
		});
		clause_webview.setDefaultHandler(defaultHandler);
		// 能够的调用JavaScript代码
		clause_webview.getSettings().setJavaScriptEnabled(true);
		clause_webview.setDownloadListener(new DownloadListener() {
			@Override
			public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
				if (url != null && url.startsWith("http://"))
					clause_webview.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
			}
		});

	}

	/**
	 * 企业政策,轻松一刻的h5
	 */
	public static void initForPolicyFunnyTime(final BridgeWebView policyWebView,final String body){
		try {
			final Context context = policyWebView.getContext();
			policyWebView.getSettings().setJavaScriptEnabled(true);
			policyWebView.setDownloadListener(new DownloadListener() {
				@Override
				public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
											long contentLength) {
					if (url != null && url.startsWith("http://"))
						context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
				}
			});
			// 加载HTML字符串进行显示
			InputStream input;
			input = context.getResources().getAssets().open("template_policy_funnytime.html");
			Document doc = Jsoup.parse(input, "UTF-8", "");
			Elements elements = doc.getElementsContainingOwnText("{{ body }}");
			for (Element link : elements) {
				link.text(StringUtils.newString(body));
			}
			// 清除jsoup的转义功能
			Entities.EscapeMode.base.getMap().clear();
			String str = doc.html();
			policyWebView.loadData(str, "text/html;charset=UTF-8", null);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
